package com.bxsy.modules.demo.test.service.impl;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import com.bxsy.modules.demo.test.entity.BxsyOrderCustomer;
import com.bxsy.modules.demo.test.entity.BxsyOrderMain;
import com.bxsy.modules.demo.test.entity.BxsyOrderTicket;
import com.bxsy.modules.demo.test.mapper.BxsyOrderCustomerMapper;
import com.bxsy.modules.demo.test.mapper.BxsyOrderMainMapper;
import com.bxsy.modules.demo.test.mapper.BxsyOrderTicketMapper;
import com.bxsy.modules.demo.test.service.IBxsyOrderMainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * @Description: 订单
 * @author： bxsy-boot
 * @date：   2019-02-15
 * @version： V1.0
 */
@Service
public class BxsyOrderMainServiceImpl extends ServiceImpl<BxsyOrderMainMapper, BxsyOrderMain> implements IBxsyOrderMainService {

	@Autowired
	private BxsyOrderMainMapper bxsyOrderMainMapper;
	@Autowired
	private BxsyOrderCustomerMapper bxsyOrderCustomerMapper;
	@Autowired
	private BxsyOrderTicketMapper bxsyOrderTicketMapper;
	
	@Override
	@Transactional
	public void saveMain(BxsyOrderMain bxsyOrderMain, List<BxsyOrderCustomer> bxsyOrderCustomerList, List<BxsyOrderTicket> bxsyOrderTicketList) {
		bxsyOrderMainMapper.insert(bxsyOrderMain);
		for(BxsyOrderCustomer entity:bxsyOrderCustomerList) {
			entity.setOrderId(bxsyOrderMain.getId());
			bxsyOrderCustomerMapper.insert(entity);
		}
		for(BxsyOrderTicket entity:bxsyOrderTicketList) {
			entity.setOrderId(bxsyOrderMain.getId());
			bxsyOrderTicketMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void updateMain(BxsyOrderMain bxsyOrderMain, List<BxsyOrderCustomer> bxsyOrderCustomerList, List<BxsyOrderTicket> bxsyOrderTicketList) {
		bxsyOrderMainMapper.updateById(bxsyOrderMain);
		
		//1.先删除子表数据
		bxsyOrderTicketMapper.deleteTicketsByMainId(bxsyOrderMain.getId());
		bxsyOrderCustomerMapper.deleteCustomersByMainId(bxsyOrderMain.getId());
		
		//2.子表数据重新插入
		for(BxsyOrderCustomer entity:bxsyOrderCustomerList) {
			entity.setOrderId(bxsyOrderMain.getId());
			bxsyOrderCustomerMapper.insert(entity);
		}
		for(BxsyOrderTicket entity:bxsyOrderTicketList) {
			entity.setOrderId(bxsyOrderMain.getId());
			bxsyOrderTicketMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		bxsyOrderMainMapper.deleteById(id);
		bxsyOrderTicketMapper.deleteTicketsByMainId(id);
		bxsyOrderCustomerMapper.deleteCustomersByMainId(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			bxsyOrderMainMapper.deleteById(id);
			bxsyOrderTicketMapper.deleteTicketsByMainId(id.toString());
			bxsyOrderCustomerMapper.deleteCustomersByMainId(id.toString());
		}
	}

}
